﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sasc.PV.BLL;
using Sasc.PV.DAL;

namespace Sasc.PV.Web.Admin.Zhuding
{
    public partial class ZDSetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetLie();
                DataBindToGridview();
 
                ////设定按钮权限
                //AddImageButton.Visible = HasPermission(GongXuQuanXianLeiXing.ADD, "101");
                //ModifyImageButton.Visible = HasPermission(GongXuQuanXianLeiXing.MODIFY);
                //DeleteImageButton.Visible = HasPermission(GongXuQuanXianLeiXing.DELETE);
                //ReportImageButton.Visible = HasPermission(GongXuQuanXianLeiXing.EXPORT);
            }
        }

        public void SetLie()  //选定表名后，绑定所有的列名
        {
            this.DropDownList2.Items.Clear();

            string PvZDPenTuTextStr = "坩埚品牌|坩埚型号|坩埚批次";
            string PvZDPenTuValueStr = "PinPai|XingHao|PiCi";

            string PvZDShaoJieTextStr = "坩埚烧结工艺名称";
            string PvZDShaoJieValueStr = "ShaoJieGongYiMingCheng";

            
            string TextStr = "";
            string ValueStr = "";
            if (this.DropDownList1.SelectedItem.Text.Trim() == "喷涂工序卡")
            {
                TextStr = PvZDPenTuTextStr;
                ValueStr = PvZDPenTuValueStr;
            }
            if (this.DropDownList1.SelectedItem.Text.Trim() == "烧结炉运转记录单")
            {
                TextStr = PvZDShaoJieTextStr;
                ValueStr = PvZDShaoJieValueStr;
            }
            

            string[] TextArray = TextStr.Split('|');
            string[] ValueArray = ValueStr.Split('|');
            for (int jj = 0; jj < TextArray.Length; jj++)
            {
                ListItem MyItem = new ListItem();
                MyItem.Text = TextArray[jj].ToString();
                MyItem.Value = ValueArray[jj].ToString();
                this.DropDownList2.Items.Add(MyItem);
            }
        }

        public void DataBindToGridview()
        {
            GVData.DataSource = DbHelperSQL.GetDataTable(
                "select * from PvZDSetting where TableName='" + this.DropDownList1.SelectedItem.Value + "' and LieName='" + this.DropDownList2.SelectedItem.Value + "' order by ID desc");
            GVData.DataKeyNames = new string[] { "ID" };
            GVData.DataBind();
        }

        protected void GVData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            PublicMethod.GridViewRowDataBound(e);
        }
        protected void GVData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                string SqlStr = "delete from PvZDSetting where ID=" + ID.ToString();
                DbHelperSQL.ExecuteSQL(SqlStr);
                DataBindToGridview();
            }
        }
        protected void GVData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
        protected void GVData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GVData.EditIndex = e.NewEditIndex;
            DataBindToGridview();
        }
        protected void GVData_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string sqlstr = "update PvZDSetting set CanShuName='"
                + ((TextBox)(GVData.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "' where ID="
                + GVData.DataKeys[e.RowIndex].Value.ToString();
            DbHelperSQL.ExecuteSQL(sqlstr);
            GVData.EditIndex = -1;
            DataBindToGridview();
        }
        protected void GVData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GVData.EditIndex = -1;
            DataBindToGridview();
        }
        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {
            DataBindToGridview();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetLie();
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (this.TextBox1.Text.Trim().Length > 0)
            {
                DbHelperSQL.ExecuteSQL("insert into PvZDSetting(TableName,LieName,CanShuName) values('" 
                    + this.DropDownList1.SelectedItem.Value + "','" 
                    + this.DropDownList2.SelectedItem.Value 
                    + "','" + this.TextBox1.Text + "')");
                MessageBox.Show(this, "参数内容添加成功！");
                this.TextBox1.Text = "";
                DataBindToGridview();
            }
        }
    }
}